const path = require('path')

const isProd = true
let externals = {}
const cdns = {
  css: [],
  js: []
}
if (isProd) {
  externals = {
    // key: 包名
    // value: 查找window上面的Vue
    vue: 'Vue',
    'element-ui': 'ELEMENT',
    xlsx: 'XLSX',
    echarts: 'echarts',
    'cos-js-sdk-v5': 'COS',
    'highlight.js': 'hljs'
  }

  cdns.js = [
    'https://unpkg.com/vue/dist/vue.js', // vuejs
    'https://unpkg.com/element-ui/lib/index.js', // element
    'https://cdn.jsdelivr.net/npm/xlsx@0.16.6/dist/xlsx.full.min.js', // xlsx 相关
    'https://cdn.jsdelivr.net/npm/xlsx@0.16.6/dist/jszip.min.js', // xlsx 相关
    'https://cdn.jsdelivr.net/npm/echarts@4.6.0/dist/echarts.min.js',
    'https://cdn.jsdelivr.net/npm/cos-js-sdk-v5/dist/cos-js-sdk-v5.min.js',
    'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/highlight.min.js'
  ]
  cdns.css = [
    'https://unpkg.com/element-ui/lib/theme-chalk/index.css'
  ]
}

module.exports = {
  lintOnSave: false,
  // --------------------------
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.

    // 排除elementUI xlsx  和 vue
    externals
  },
  // ------------------------------------------------
  chainWebpack: config => {
    const svgRule = config.module.rule('svg')
    // ===================
    config
      .plugin('html')
      .tap(args => {
        args[0].cdns = cdns
        return args
      })
    // ======================
    svgRule.uses.clear()
    svgRule
      .test(/.svg$/)
      .include.add(path.resolve(__dirname, './src/icons/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
    const fileRule = config.module.rule('file')
    fileRule.uses.clear()
    fileRule
      .test(/.svg$/)
      .exclude.add(path.resolve(__dirname, './src/icons/svg'))
      .end()
      .use('file-loader')
      .loader('file-loader')
  }
}
